function* gen() {
    while (true) {
        try {
            yield 42;
        } catch (e) {
        console.log("Error caught!");
        }
    }
}

const g = gen();
g.next();
// { value: 42, done: false }
y = g.throw(new Error("Something went wrong"));
// "Error caught!"
// { value: 42, done: false }
